/** @brief Define kangmodb infra-structure macro, function, classes.
 *
 *  kdInfra.h
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 4. 11..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */

#ifndef _KD_INFRA_H_
#define _KD_INFRA_H_ (1)

#include "sqliteInt.h"
#include <string>

/** @brief The exception enumeration type used in KangmoDB 
 */
typedef enum kdExcp {
    KD_SUCCESS = 0, 
	KD_EXCP_KEY_LOCKED,
	KD_EXCP_KEY_EXISTS,
	KD_EXCP_KEY_NOT_FOUND,
	KD_EXCP_NO_MEM,
	KD_EXCP_ARRAY_MAP_FULL,
	KD_EXCP_HIT_MAX_TABLE_COUNT,
	KD_EXCP_MEM_CHUNK_SIZE_NOT_POWER_OF_TWO,
	KD_EXCP_MEM_CHUNK_NOT_ALIGNED,
	KD_EXCP_CHUNK_MEM_OUT_OF_FREESLOTS,
	KD_EXCP_CREATE_REGION_FILE,
	KD_EXCP_OPEN_REGION_FILE,
	KD_EXCP_CLOSE_REGION_FILE,
	KD_EXCP_DELETE_REGION_FILE,
	KD_EXCP_STRETCH_REGION_FILE,
	KD_EXCP_INVALID_REGION_FILE,
	KD_EXCP_MAP_REGION_FILE,
	KD_EXCP_REGION_NOT_OPEN,
	KD_EXCP_UNMAP_REGION_FILE,
	KD_EXCP_REGION_NO_MORE_CHUNK,
	/** @brief The shared region is mapped to a different address. 
	 * The shared region contains pointers based on the mapped address, so it should be mapped to the same address whenever it is open.
	 */
	KD_EXCP_DIFFERNT_MAP_ADDR
} kdExcp;

#define KD_VOID kdExcp

#define KD_TRY kdExcp _excp = KD_SUCCESS;

#define KD_CATCH goto _finally; _catch:

#define KD_CAUGHT(excp) if (_excp == (excp))

#define KD_CONVERT(org_excp, converted_excp) if (_excp == (org_excp)) _excp = (converted_excp);

#define KD_FINALLY _finally:

#define KD_THROW(excp) { _excp=excp; goto _catch; }

#define KD_END { return _excp; goto _catch; goto _finally; }

#define KD_RETURN { _excp=KD_SUCCESS; goto _finally; }

#define KD_CALL(x) if ( (_excp=(x)) != KD_SUCCESS ) { KD_THROW(_excp); }

#define KD_ASSERT(x) if (!(x)) { printf("Assertion(%s) failure at %s:%d\n", #x, __FILE__, __LINE__); abort(); }

#if defined(_DEBUG)
#  define KD_DASSERT(x) KD_ASSERT(X)
#else
#  define KD_DASSERT(x)
#endif

#define KD_INLINE inline

#endif /*_KD_INFRA_H_*/